package com.cloudweb.oa.service;

import cn.js.fan.db.ListResult;
import cn.js.fan.db.ResultIterator;
import cn.js.fan.db.ResultRecord;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.oa.bean.Address;
import com.cloudweb.oa.dao.AddressDao;
import com.cloudweb.oa.security.AuthUtil;
import com.cloudweb.oa.vo.Result;
import com.cloudwebsoft.framework.db.JdbcTemplate;
import com.cloudwebsoft.framework.util.LogUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.redmoon.oa.db.SequenceManager;
import com.redmoon.oa.pvg.Privilege;
import com.redmoon.oa.visual.ModuleSetupDb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

/**
 * @Author: qcg
 * @Description:
 * @Date: 2018/12/28 14:34
 */
@Service
public class ModelService {

	@Autowired
	private AuthUtil authUtil;

	public JSONObject getLikeCount(String moduleCode, Long moduleId, Integer mode) throws ErrMsgException {
		ModuleSetupDb msd = new ModuleSetupDb();
		msd = msd.getModuleSetupDb(moduleCode);
		JSONObject json = new JSONObject();
		try {
			if (mode == 1) {
				String sql = "select count(*) from ft_give_like where form_code=? and module_id=? and val=?";
				JdbcTemplate jt = new JdbcTemplate();
				ResultIterator ri = jt.executeQuery(sql, new Object[]{msd.getFormCode(), moduleId, 0});
				if (ri.hasNext()) {
					ResultRecord rr = ri.next();
					json.put("count", rr.getInt(1));
				}

				sql = "select count(*) from ft_give_like where form_code=? and module_id=? and val=? and user_name=?";
				ri = jt.executeQuery(sql, new Object[]{msd.getFormCode(), moduleId, 0, authUtil.getUserName()});
				if (ri.hasNext()) {
					ResultRecord rr = ri.next();
					json.put("myCount", rr.getInt(1));
				}
			} else {
				JdbcTemplate jt = new JdbcTemplate();
				String sql = "select count(*) from ft_give_like where form_code=? and module_id=? and val=?";
				ResultIterator ri = jt.executeQuery(sql, new Object[]{msd.getFormCode(), moduleId, 1});
				if (ri.hasNext()) {
					ResultRecord rr = ri.next();
					json.put("count1", rr.getInt(1));
				}

				sql = "select count(*) from ft_give_like where form_code=? and module_id=? and val=?";
				ri = jt.executeQuery(sql, new Object[]{msd.getFormCode(), moduleId, 3});
				if (ri.hasNext()) {
					ResultRecord rr = ri.next();
					json.put("count3", rr.getInt(1));
				}

				sql = "select count(*) from ft_give_like where form_code=? and module_id=? and val=?";
				ri = jt.executeQuery(sql, new Object[]{msd.getFormCode(), moduleId, 5});
				if (ri.hasNext()) {
					ResultRecord rr = ri.next();
					json.put("count5", rr.getInt(1));
				}

				sql = "select val from ft_give_like where form_code=? and module_id=? and user_name=?";
				ri = jt.executeQuery(sql, new Object[]{msd.getFormCode(), moduleId, authUtil.getUserName()});
				if (ri.hasNext()) {
					ResultRecord rr = ri.next();
					json.put("myVal", rr.getInt(1));
				}
			}
		} catch (SQLException e) {
			LogUtil.getLog(getClass()).error(e);
			json.put("count", 0);
			json.put("myCount", 0);
			json.put("count1", 0);
			json.put("count2", 0);
			json.put("count3", 0);
			json.put("myVal", 0);
		}
		return json;
	}
}
